<template>
  <el-dialog :title="data.title" :close-on-click-modal="false" @close="close" :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="form" size="mini">
      <el-row>
        <el-col :span = 24>
          <el-form-item label="账号对应支付科目" :label-width="formLabelWidth" prop="payItem">
            <el-select v-model ="form.payItem" clearable>
              <el-option v-for="item in payItemIdOptions"  :key="item.key" :label="item.value" :value="item.key"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="支付账号" :label-width="formLabelWidth" prop="account">
            <el-input v-model="form.account"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="支付密码" :label-width="formLabelWidth" prop="accountPwd">
            <el-input v-model="form.accountPwd" type="password"></el-input>
            交易密码加密保存，如果要修改请输入原始密码
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="支付账号类型" :label-width="formLabelWidth" prop="accountType">
            <el-select v-model="form.accountType"  clearable >
              <el-option v-for="item in accountTypeOptions" :key="item.key" :label="item.value" :value="item.key"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="商户标识" :label-width="formLabelWidth" prop="merCode">
            <el-input v-model="form.merCode"></el-input>
            付款商户标示，由易宝提供（目前仅用于G5支付）
          </el-form-item>
        </el-col>
        <el-col :span = 24>
          <el-form-item label="加密key" :label-width="formLabelWidth" prop="merKey">
            <el-input v-model="form.merKey"></el-input>
            加密用key，由易宝提供（目前仅用于G5支付）
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <c-button type="primary" @click="btnOk" v-if="data.showsave">确 定</c-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'SDialogEdit',
  props: {
    data: {
      type: Object,
      default () {
        return {
          detail: {},
          title: '',
          showsave: true
        }
      }
    }
  },
  data () {
    return {
      formLabelWidth: '120px',
      visible: true,
      payItemIdOptions: [],
      accountTypeOptions: [],
      form: {
        id: this.data.detail.id,
        payItem: this.data.detail.payItem,
        status: this.data.detail.status,
        account: this.data.detail.account,
        accountType: this.data.detail.accountType,
        accountPwd: this.data.detail.accountPwd,
        merCode: this.data.detail.merCode,
        merKey: this.data.detail.merKey
      },
      rules: null
    }
  },
  mounted: function () {
    this.getSelectData()
  },
  methods: {
    close () {
      this.$emit('close')
    },
    btnOk (btn) {
      this.$refs['form'].validate((valid) => {
        // 校验通过
        if (valid) {
          if (!this.form.id) {
            this.$service.airB2bPayAccount.addAirB2bPayAccount(this.form).then(() => {
              this.$emit('ok')
              this.visible = false
            }).finally(() => {
              btn.isLoading(false)
            })
          } else {
            this.$service.airB2bPayAccount.updateAirB2bPayAccount(this.form).then(() => {
              this.$emit('ok')
              this.visible = false
            }).finally(() => {
              btn.isLoading(false)
            })
          }
        } else {
          btn.isLoading(false)
        }
      })
    },
    getSelectData () {
      this.$service.options.getPayItem().then(res => {
        this.payItemIdOptions = res.data
      })
      this.$service.options.getYeePayAccountType().then(res => {
        this.accountTypeOptions = res.data
      })
    }
  }
}
</script>

<style>

</style>
